package com.xxx.car.web;

import com.xxx.car.entity.vo.request.ReqUserLogin;
import com.xxx.car.entity.vo.request.ReqUserRegister;
import com.xxx.car.service.BannerService;
import com.xxx.car.service.UserService;
import com.xxx.common.base.vo.APIResponse;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yxj on 17/6/15.
 */
@RestController
@Api(value = "/common", description = "APP公共接口[无需用户登陆]")
@RequestMapping("/common")
public class AppCommonController {
	@Autowired
	UserService userService;
	@Autowired
	BannerService bannerService;

	/**
	 * 发送验证码
	 *
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/sendValidCode")
	public APIResponse<?> sendValidCode(@RequestParam String phone) {
		if (StringUtils.isEmpty(phone)){
			return APIResponse.returnFail("手机号必填");
		}
		String mobile = phone.trim();
		userService.sendValidCodetToPhone(mobile,60,60);
		return APIResponse.returnSuccess();
	}

	/**
	 * 用户验证码注册
	 * @param reqUserRegister
	 * @return
	 */
	@RequestMapping(value = "register")
	public APIResponse<?> register(@ModelAttribute ReqUserRegister reqUserRegister){
		APIResponse response = userService.userRegister(reqUserRegister);
		return response;
	}

	/**
	 * 用户登陆
	 * @param reqUserLogin
	 * @return
	 */
	@RequestMapping(value = "login")
	public APIResponse<?> login(@ModelAttribute ReqUserLogin reqUserLogin){
		APIResponse response = userService.userLogin(reqUserLogin);
		return response;
	}

	/**
	 * 用户忘记密码
	 * @param reqUserRegister
	 * @return
	 */
	@RequestMapping(value = "passwordLost")
	public APIResponse<?> passwordLost(@ModelAttribute ReqUserRegister reqUserRegister){
		APIResponse response = null;
		try {
			response = userService.passwordLost(reqUserRegister);
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(e.getMessage());
		}
		return response;
	}


	/**
	 * 首页banner
	 * @return
	 */
	@RequestMapping(value = "banner")
	public APIResponse<?> banner(){
		APIResponse response = bannerService.queryBannerListByApp();
		return response;
	}

}
